import { Body, Controller, Get, Param, Post, Put, Query } from '@nestjs/common';
import { MessageService } from './message.service';
import { CreateMessageDto } from './dto/create-message.dto';
import { QueryMessageDto } from './dto/query-message.dto';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('消息管理')
@ApiBearerAuth()
@Controller('message')
@RequireLogin()
export class MessageController {
  constructor(private readonly messageService: MessageService) {}

  @Post()
  @ApiOperation({ summary: '发送消息' })
  @ApiResponse({ status: 201, description: '发送成功' })
  async create(
    @Body() createMessageDto: CreateMessageDto,
    @UserInfo('userId') userId: number,
  ) {
    return await this.messageService.create(createMessageDto, userId);
  }

  @Get()
  @ApiOperation({ summary: '查询消息列表' })
  async findAll(
    @Query() queryMessageDto: QueryMessageDto,
    @UserInfo('userId') userId: number,
  ) {
    return await this.messageService.findAll(queryMessageDto, userId);
  }

  @Get('unread/count')
  @ApiOperation({ summary: '获取未读消息数量' })
  async getUnreadCount(@UserInfo('userId') userId: number) {
    return await this.messageService.getUnreadCount(userId);
  }

  @Put(':id/read')
  @ApiOperation({ summary: '标记消息为已读' })
  @ApiParam({ name: 'id', description: '消息ID' })
  @ApiResponse({ status: 200, description: '标记成功' })
  async markAsRead(
    @Param('id') id: string,
    @UserInfo('userId') userId: number,
  ) {
    return await this.messageService.markAsRead(+id, userId);
  }

  @Put('read/all')
  @ApiOperation({ summary: '标记所有消息为已读' })
  @ApiResponse({ status: 200, description: '标记成功' })
  async markAllAsRead(@UserInfo('userId') userId: number) {
    return await this.messageService.markAllAsRead(userId);
  }
}
